package com.cch.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

public class HttpResult {
    private static Logger logger = LoggerFactory.getLogger(HttpResult.class);

    private int code;
    private String body;
    private Map<String, String> headerMap;

    public String makeMsg() {
        return "CODE:" + code + ",BODY:" + body;
    }

    public void changeEncode(String from, String to) {
        if (body != null) {
            try {
                body = new String(body.getBytes(from), to);
            } catch (Exception e) {
                logger.error("HttpResult changeEncode error!", e);
            }
        }
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }

    public Map<String, String> getHeaderMap() {
        return headerMap;
    }

    public void setHeaderMap(Map<String, String> headerMap) {
        this.headerMap = headerMap;
    }

    /**
     * @return
     */
    public boolean isSuccess() {
        return code == 200;
    }

    public boolean isTimeout() {
        return code == 499 || code == 502 || code == 504 || code == 498;
    }

    @Override
    public String toString() {
        return "HttpResult{" +
                "code=" + code +
                ", body='" + body + '\'' +
                ", headerMap=" + headerMap +
                '}';
    }
}
